package com.astn.bks;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONValue;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.astn.adapter.booking.CRequestAdapter;
import com.astn.adapter.booking.PRequestAdapter;
import com.astn.bks.util.BksConstants;
import com.astn.bks.util.JsonUtils;
import com.astn.bks.util.WebserviceUtils;
import com.astn.entity.booking.QuoteRequest;
import com.google.gson.Gson;

public class PLatestRequest extends Activity {
	private ListView lsvTop;
	private Button btnNext;
	private Button btnBack;
	private TextView txvPage;
	private ArrayList<QuoteRequest> lstRequest;// Top destination
	private int currentpage = 0;
	private int totalPage = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.platestrequest);
		lsvTop = (ListView) findViewById(R.id.lsvTop);
		btnNext = (Button) findViewById(R.id.btnNextPage);
		btnBack = (Button) findViewById(R.id.btnPreviousPage);
		txvPage = (TextView) findViewById(R.id.txvPage);
		getNumberPage();
		txvPage.setText(String.valueOf(currentpage + 1) + "/"
				+ String.valueOf(totalPage));
		// reloadTopDestination();
		reloadList();
		btnNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// getNumberPageDestination();
				getNumberPage();
				if (currentpage < totalPage - 1) {
					currentpage++;
				} else {
					currentpage = 0;
				}
				txvPage.setText(String.valueOf(currentpage + 1) + "/"
						+ String.valueOf(totalPage));
				// reloadTopDestination();
				reloadList();
			}

		});
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// getNumberPageDestination();
				getNumberPage();
				if (currentpage > 0) {
					currentpage--;
				} else {
					currentpage = totalPage - 1;
				}
				txvPage.setText(String.valueOf(currentpage + 1) + "/"
						+ String.valueOf(totalPage));
				// reloadTopDestination();
				reloadList();
			}

		});
	}

	private void getNumberPage() {
		Object obj = null;
		Map<String, Object> mapPara = new HashMap<String, Object>();
		mapPara.put("PAGESIZE", BksConstants.PAGESIZE);
		try {
			obj = WebserviceUtils.call("com_xmlrpc",
					"getNumberPageLatestRequest", mapPara);
			String str = (String) obj;
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			Float f = Float.parseFloat(obj.toString());
			totalPage = f.intValue();
		} catch (Exception e) {
			totalPage = 0;
		}
	}

	private void reloadList() {
		lstRequest = new ArrayList<QuoteRequest>();
		Object resultObj = null;
		Map<String, Object> mapPara = new HashMap<String, Object>();
		mapPara.put("PAGE", currentpage);
		mapPara.put("PAGESIZE", BksConstants.PAGESIZE);
		try {
			resultObj = WebserviceUtils.call("com_xmlrpc", "getLatestRequest",
					mapPara);
			String str = (String) resultObj;
		} catch (Exception e) {

		}
		if (resultObj != null) {
			QuoteRequest[] gArr = null;
			try {
				gArr = JsonUtils.gson.fromJson((String) resultObj,
						QuoteRequest[].class);
			} catch (Exception e) {
				e.printStackTrace();
			}
			lstRequest = new ArrayList<QuoteRequest>();
			if (gArr != null) {
				lstRequest.addAll(Arrays.asList(gArr));
			}
			// if (arr != null) {
			// for (int i = 0; i < arr.size(); i++) {
			// JSONObject obj = (JSONObject) arr.get(i);
			// try {
			// QuoteTown tmpTown = new QuoteTown();
			// tmpTown.setCitycode((String) obj.get("citycode"));
			// tmpTown.setCityname((String) obj.get("cityname"));
			// tmpTown.setCitynumber((String) obj.get("citynumber"));
			// tmpTown.setCountryid(Integer.parseInt((String) obj
			// .get("countryid")));
			// tmpTown.setId(Integer.parseInt((String) obj.get("id")));
			// tmpTown.setRegionid(Integer.parseInt((String) obj
			// .get("regionid")));
			// tmpTown.setStateid(Integer.parseInt((String) obj
			// .get("stateid")));
			// lstTown.add(tmpTown);
			// } catch (Exception e) {
			// // TODO: handle exception
			// }
			// }
			// }
		}

		PRequestAdapter adap = new PRequestAdapter(this, lstRequest);
		lsvTop.setAdapter(adap);

	}
}
